package com.example.rabbit.eight;

import com.example.rabbit.util.RabbitMqUtils;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;

/**
 * 消费者1
 *
 * @Author ifredomvip@gmail.com
 * @Date 2022/8/9 14:10
 * @Version 1.0.0
 * @Description
 **/
public class DeadConsumer1 {
    public static String normalExchangeName = "normal_exchange";
    public static String deadExchangeName = "dead_exchange";
    public static String normalQueueName = "normal_queue";
    public static String deadQueueName = "dead_queue";

    public static void main(String[] args) throws IOException, TimeoutException {
        // 死信触发场景1和2：ttl超时，max-limit限制长度
        ttlAndLimitHandle();
        //死信触发场景3：basicReject拒绝处理消息
//        rejectHandle();
    }
    /**
     * 死信触发场景1和2：ttl超时，max-limit限制长度
     *
     * @throws IOException Ioexception
     * @throws TimeoutException 超时异常
     */
    public static void ttlAndLimitHandle() throws IOException, TimeoutException {
        Channel channel = RabbitMqUtils.getChannel();
        channel.exchangeDeclare(normalExchangeName, BuiltinExchangeType.DIRECT);
        channel.exchangeDeclare(deadExchangeName, BuiltinExchangeType.DIRECT);

        String message = "test信息";

        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl", 10000);
        arguments.put("x-dead-letter-exchange", deadExchangeName);
        arguments.put("x-dead-letter-routing-key", "deadRoutingKey");
        arguments.put("x-max-length", 6);


        channel.queueDeclare(normalQueueName, false, false, false, arguments);
        channel.queueDeclare(deadQueueName, false, false, false, null);


        // 绑定
        channel.queueBind(normalQueueName, normalExchangeName, "normalRoutingKey");
        channel.queueBind(deadQueueName, deadExchangeName, "deadRoutingKey");

        System.out.println("等待接收消息...");
        channel.basicConsume(normalQueueName, true, (consumeTag, delivery) -> {
            String receiveMessage = new String(delivery.getBody(), StandardCharsets.UTF_8);

            System.out.println("DeadConsumer1 收到消息" + receiveMessage);
        }, (consumeTag) -> {
            System.out.println("没收到消息");
        });
    }

    /**
     * 死信触发场景3：basicReject拒绝处理消息
     *
     * @throws IOException Ioexception
     * @throws TimeoutException 超时异常
     */
    public static void rejectHandle() throws IOException, TimeoutException {
        Channel channel = RabbitMqUtils.getChannel();
        channel.exchangeDeclare(normalExchangeName, BuiltinExchangeType.DIRECT);
        channel.exchangeDeclare(deadExchangeName, BuiltinExchangeType.DIRECT);

        String message = "test信息";

        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl", 10000);
        arguments.put("x-dead-letter-exchange", deadExchangeName);
        arguments.put("x-dead-letter-routing-key", "deadRoutingKey");
        arguments.put("x-max-length", 6);


        channel.queueDeclare(normalQueueName, false, false, false, arguments);
        channel.queueDeclare(deadQueueName, false, false, false, null);


        // 绑定
        channel.queueBind(normalQueueName, normalExchangeName, "normalRoutingKey");
        channel.queueBind(deadQueueName, deadExchangeName, "deadRoutingKey");

        System.out.println("等待接收消息...");
        // 第二个参数 是否自动应答设置为false
        channel.basicConsume(normalQueueName, false, (consumeTag, delivery) -> {
            String receiveMessage = new String(delivery.getBody(), StandardCharsets.UTF_8);

            if ("test信息5".equals(receiveMessage)) {
                System.out.println("拒绝处理 信息5");
                // 第二个参数false。是否放回原队列
                channel.basicReject(delivery.getEnvelope().getDeliveryTag(), false);
            } else {
                System.out.println("DeadConsumer1 收到消息" + receiveMessage);
                channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
            }

        }, (consumeTag) -> {
            System.out.println("没收到消息");
        });

    }
}
